<?php
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();

jimport( 'joomla.application.component.model' );

class NvModelAPIKey extends JModel
{

	var $_id = null;
	var $_data = null;
	

	function __construct(){
		parent::__construct();
		$array = JRequest::getVar('cid', array(0), '', 'array');
		$this->setId($array[0]);
				
		global $mainframe;
		$limit = $mainframe->getUserStateFromRequest( 'limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
		$limitstart = $mainframe->getUserStateFromRequest( 'limitstart', 'limitstart', 0, 'int' );

		$this->setState('limit', $limit);
		$this->setState('limitstart', $limitstart);			
	}

	function setId($id_api){
		// Set weblink id and wipe data
		$this->_id		= $id_api;
		$this->_data	= null;
		$this->_total 	= null;
	}
	
	// function ini mengambil data yang akan ditampilkan menggunakan cURL
	function getDatasCurl(){
	
		$task = JRequest::getVar('task');
			
        $config_params =& JComponentHelper::getParams( 'com_nv' );
		$url_server = $config_params->get('url_server_api');
		//echo $url_server;
		
		
		if (!isset($task) || empty($task) || !empty( $this->_data )) {
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL,$url_server);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($ch, CURLOPT_HEADER, 0);
			$data = curl_exec($ch);
			curl_close($ch);
			
			$this->_data = $this->fetch_xml($data);	
			
			//return $this->_data;
		} 
		
		$this->_total = count($this->_data)/2;
		//echo "<br>count data = ".count($this->_data);
		return $this->_data;	
    }
	
	function fetch_xml($xml){

		if(is_file($xml)){
			$xml_data = file_get_contents($xml);
		}
		else{
			$xml_data = $xml;
		}
		
		$parser = xml_parser_create();
		xml_parse_into_struct($parser, $xml_data, &$assoc_arr, &$idx_arr);
		xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
		xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
		$root_tag = $assoc_arr[0]['tag'];
		$base_tag = strtolower($assoc_arr[1]['tag']);
		$i = 0;
		foreach($assoc_arr as $key => $element){
			if($element['tag'] != $root_tag){
				if(!preg_match('/^\s+$/', $element['value'])){
					$tag = strtolower($element['tag']);
					$items[$i][$tag] = $element['value'];
					if($tag == $base_tag){
						$i++;
					}
				}	
				elseif(isset($element['attributes'])){
					$items[$i]['id'] = $element['attributes']['ID'];
				}
			}
		}

		return $items;
    } //end of function fetchxml
	
	
	
	function delete(){
		// mengambil id yang terpilih dari halaman view
		$cids = JRequest::getVar( 'cid', array(0), 'post', 'array' );	
		
		// mengambil alamat REST server dari parameter yang telah disimpan
		$config_params =& JComponentHelper::getParams( 'com_nv' );
		$url_server = $config_params->get('url_server_api');
		
		// deklarasi variabel $recsno yang akan berisi id dari record yang ingin dihapus
		$recsno = null;
		
		foreach($cids as $key=>$value){
			$array_value[]=$value;	
		}
		// mengisi variabel recsno dengan id record yang ingin dihapus, dipisahkan dengan koma
		$recsno = join(',',$array_value);
		
		// menyusun array yang akan dikirim ke REST server melalui cURL
		// array ini berisi id record yang ingin dihapus, flag, dan URL REST server
		$data = array('recsno'=>$recsno, 'flag'=>"delete", 'url'=>$url_server);
		
		//echo "<br>ini data : ";
		//print_r($data);
		
		//mengirim data ke restserver dengan cURL	  
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL,$url_server );
		curl_setopt($ch, CURLOPT_POST, true);
		curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
		curl_exec($ch);
      
		curl_close($ch);
		
		return true;
	}
	
	function getPagination(){
		if (empty($this->_pagination)){
			jimport('joomla.html.pagination');
			$this->_pagination = new JPagination( $this->_total, $this->getState('limitstart'), $this->getState('limit') );
		}
		return $this->_pagination;
	}	
	
	
	
}
